﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using YArchitech.LIB;

namespace YArchitech.Plumbing
{
	internal class HYPumpDataXMLInfo
	{
		public HYPumpDataXMLInfo(string path)
		{
			this.path = path;
		}

		public List<HYPumpData> LoadPumpSelectionResult(List<HYPumpData> m_arrSinglePumpsList)
		{
			if (!File.Exists(this.path) && File.Exists(Path.Combine(Product.DataLocation, "User\\HYPumpSelectionResult.XML")))
			{
				File.Copy(Path.Combine(Product.DataLocation, "User\\HYPumpSelectionResult.XML"), this.path);
			}
			List<XmlElement> allElements = XMLOperating.GetAllElements(XMLOperating.LoadXml(this.path, "ResultList"), "ResultList/SinglePumpList");
			if (allElements.Count > 0)
			{
				foreach (XmlElement xmlElement in allElements)
				{
					string attribute = xmlElement.GetAttribute("SPEC");
					HYPumpData hypumpBySPEC = this.GetHYpumpBySPEC(attribute);
					m_arrSinglePumpsList.Add(hypumpBySPEC);
				}
			}
			return m_arrSinglePumpsList;
		}

		public List<HYGroupPumpData> LoadPumpGroupSelectionResult(List<HYGroupPumpData> m_arrGroupPumpsList)
		{
			if (!File.Exists(this.path) && File.Exists(Path.Combine(Product.DataLocation, "User\\HYPumpSelectionResult.XML")))
			{
				File.Copy(Path.Combine(Product.DataLocation, "User\\HYPumpSelectionResult.XML"), this.path);
			}
			XmlDocument xmlDoc = XMLOperating.LoadXml(this.path, "ResultList");
			foreach (XmlElement xmlElement in XMLOperating.GetAllElements(xmlDoc, "ResultList/GroupPumpList"))
			{
				bool bIsParallel = true;
				List<HYPumpData> list = new List<HYPumpData>();
				if (xmlElement.GetAttribute("IsPara") == "false")
				{
					bIsParallel = false;
				}
				List<XmlElement> allElements = XMLOperating.GetAllElements(xmlDoc, xmlElement);
				if (allElements.Count > 0)
				{
					foreach (XmlElement pump in allElements)
					{
						this.AddListPumpData(pump, list);
					}
				}
				list = list.Distinct<HYPumpData>().ToList<HYPumpData>();
				HYGroupPumpData hygroupPumpData = new HYGroupPumpData();
				hygroupPumpData.m_strGroupName = xmlElement.GetAttribute("GroupName");
				hygroupPumpData.m_bIsParallel = bIsParallel;
				hygroupPumpData.m_arrData.AddRange(list);
				m_arrGroupPumpsList.Add(hygroupPumpData);
			}
			return m_arrGroupPumpsList;
		}

		private void AddListPumpData(XmlElement pump, List<HYPumpData> listPumpdataTable)
		{
			HYPumpData item = new HYPumpData();
			string attribute = pump.GetAttribute("SPEC");
			item = this.GetHYpumpBySPEC(attribute);
			listPumpdataTable.Add(item);
		}

		private HYPumpData GetHYpumpBySPEC(string sSPEC)
		{
			foreach (HYPumpData hypumpData in HYPumpDataManager.GetAllPumpData())
			{
				if (sSPEC == hypumpData.StrSpecification)
				{
					return hypumpData;
				}
			}
			return null;
		}

		public bool SavePumpSelectionResult(TreeView tvResultPumps)
		{
			XmlDocument xmlDocument = XMLOperating.LoadXml(this.path, "ResultList");
			XmlElement xmlElement = XMLOperating.AddElement(xmlDocument, this.path, "ResultList", "SinglePumpList");
			XmlElement xmlElement2 = XMLOperating.AddElement(xmlDocument, this.path, "ResultList", "GroupPumpList");
			int num = 0;
			foreach (object obj in tvResultPumps.Nodes)
			{
				foreach (object obj2 in ((TreeNode)obj).Nodes)
				{
					TreeNode treeNode = (TreeNode)obj2;
					if (num == 0)
					{
						XmlElement xmlElement3 = xmlDocument.CreateElement("Pump");
						xmlElement.AppendChild(xmlElement3);
						HYPumpData hypumpData = treeNode.Tag as HYPumpData;
						XMLOperating.AddAttribute(xmlElement3, "SPEC", hypumpData.StrSpecification);
						XMLOperating.AddAttribute(xmlElement3, "Type", hypumpData.StrBaseType);
						XMLOperating.AddAttribute(xmlElement3, "StructType", hypumpData.StrStructType);
						XMLOperating.AddAttribute(xmlElement3, "FitType", hypumpData.StrFitType);
						XMLOperating.AddAttribute(xmlElement3, "Volume", hypumpData.DQ.ToString());
						XMLOperating.AddAttribute(xmlElement3, "Head", hypumpData.DH.ToString());
						XMLOperating.AddAttribute(xmlElement3, "Efficient", hypumpData.DEffection.ToString());
						XMLOperating.AddAttribute(xmlElement3, "EnginPower", hypumpData.DEnginPower.ToString());
						XMLOperating.AddAttribute(xmlElement3, "Use", hypumpData.StrUse);
						XMLOperating.AddAttribute(xmlElement3, "Factory", hypumpData.StrFactory);
						XMLOperating.AddAttribute(xmlElement3, "TypeInFactory", hypumpData.StrTypeInFactory);
					}
					else
					{
						XmlElement xmlElement4 = xmlDocument.CreateElement("GroupPump");
						xmlElement2.AppendChild(xmlElement4);
						HYGroupPumpData hygroupPumpData = treeNode.Tag as HYGroupPumpData;
						string attValue = hygroupPumpData.m_bIsParallel ? "true" : "false";
						XMLOperating.AddAttribute(xmlElement4, "GroupName", treeNode.Text);
						XMLOperating.AddAttribute(xmlElement4, "IsPara", attValue);
						foreach (HYPumpData hypumpData2 in hygroupPumpData.m_arrData)
						{
							XmlElement xmlElement5 = xmlDocument.CreateElement("Pump");
							xmlElement4.AppendChild(xmlElement5);
							XMLOperating.AddAttribute(xmlElement5, "SPEC", hypumpData2.StrSpecification);
							XMLOperating.AddAttribute(xmlElement5, "Type", hypumpData2.StrBaseType);
							XMLOperating.AddAttribute(xmlElement5, "StructType", hypumpData2.StrStructType);
							XMLOperating.AddAttribute(xmlElement5, "FitType", hypumpData2.StrFitType);
							XMLOperating.AddAttribute(xmlElement5, "Volume", hypumpData2.DQ.ToString());
							XMLOperating.AddAttribute(xmlElement5, "Head", hypumpData2.DH.ToString());
							XMLOperating.AddAttribute(xmlElement5, "Efficient", hypumpData2.DEffection.ToString());
							XMLOperating.AddAttribute(xmlElement5, "EnginPower", hypumpData2.DEnginPower.ToString());
							XMLOperating.AddAttribute(xmlElement5, "Use", hypumpData2.StrUse);
							XMLOperating.AddAttribute(xmlElement5, "Factory", hypumpData2.StrFactory);
							XMLOperating.AddAttribute(xmlElement5, "TypeInFactory", hypumpData2.StrTypeInFactory);
						}
					}
				}
				num++;
			}
			xmlDocument.Save(this.path);
			return true;
		}

		private string path = "";

		private const string xmlRootNode = "ResultList";
	}
}
